package com.openNetty.server.core;

import com.openNetty.common.system.LoggerUtil;
import com.openNetty.common.utils.FileTransUtil;
import com.openNetty.server.nettyserver.Global;
import com.openNetty.server.sftp.SftpConnect;

import java.io.File;

/**
 * sftp 下载执行类
 * @author zhenggm
 * @create 2017-12-07 下午 2:26
 **/


public class SftpExector {

    private static LoggerUtil logger = LoggerUtil.getLogger(SftpExector.class);
    private static SftpConnect sftp = SftpConnect.getInstance();

    public boolean downSqlData() {
        String localPath = Global.properties.getProperty("local.downpath") + "/down";
        String directory = Global.properties.getProperty("sftp.root");
        File file = new File(localPath);
        if (file.exists()) {
            if (!file.isDirectory()) {
                logger.debug("the same name file exists, can not create dir");
            }
        } else {
            logger.debug("dir not exists, create it ...");
            file.mkdir();
        }
        // 清空文件夹
        logger.debug("开始清空文件夹");
        
        FileTransUtil.delAllFile(localPath);
        boolean flag = false;
        try {
            logger.debug("开始连接sftp下载文件");
            sftp.connect();
            sftp.downloadAllDirectory(directory, localPath);
            sftp.disconnect();
            logger.debug("sftp文件下载完成");
            flag = true;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return flag;
    }

}
